package com.fnd.med.zsms.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.fnd.med.zsms.Constant;
import com.fnd.med.zsms.bean.AppVersion;
import com.fnd.med.zsms.bean.User;
import com.fnd.med.zsms.service.AppService;
import com.fnd.med.zsms.service.UserService;

/**
 * Servlet implementation class UserServlet
 */
@Named("userServlet")
public class UserServlet extends HttpServlet {
	Logger logger = Logger.getLogger(UserServlet.class);
	
	private static final long serialVersionUID = 1L;
    @Inject
	private UserService userService;
	
    @Inject
	private AppService appService;
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/json;charset=utf-8");
		String uri = request.getRequestURI();
		
		logger.debug(uri);
		System.out.println(uri);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if("/zsms/user/validateUser".equals(uri)){
			String userName = request.getParameter("userName");
			String password = request.getParameter("password");
			
			User user = userService.getUser(userName, password);
			
			if(null!=user){
				
				request.getSession().setAttribute(Constant.USER_INFO, user);
				
				resultMap.put("result", Constant.RESULT_SUCCESS);
			}else{
				resultMap.put("result", Constant.RESULT_FAIL);
			}
		}else if("/zsms/user/changePassword".equals(uri)){
			 
			String oldPassword = request.getParameter("oldPassword");
			String newPassword = request.getParameter("newPassword");
			User user = (User) request.getSession().getAttribute(Constant.USER_INFO);
			if(null!=user){
				if(oldPassword.equals(user.getPassword())){
					user = userService.changePassword(user, newPassword);
					request.getSession().setAttribute(Constant.USER_INFO, user);
					resultMap.put("result", Constant.RESULT_SUCCESS);
				}else{
					resultMap.put("result", Constant.RESULT_FAIL);
				}
				
			}else{
				resultMap.put("result", Constant.RESULT_FAIL);
			}
		}else if("/zsms/user/validateApp".equals(uri)){
			String appName = request.getParameter("appName");
			
			String appVersion = request.getParameter("appVersion");
			AppVersion av =  appService.getLastVersion(appName);
			
			if(appVersion.compareTo(av.getAppVersion())!=0){
				resultMap.put("result", Constant.RESULT_SUCCESS);
				
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.setExcludes(new String[]{"seq","appSeq"});
				
				resultMap.put("status", JSONObject.fromObject(av, jsonConfig));
			}else{
				resultMap.put("result", Constant.RESULT_FAIL);
			}
		}
		
		JSONObject json = JSONObject.fromObject(resultMap);
		
		PrintWriter out = response.getWriter();
		
		out.print(json.toString());
		out.flush();
		out.close();
	}

}
